/*
    订单管理:奖金兑换订单
 */
$(function () {
    $("#jqGrid").Grid({
        url: '../orderamount/list?payWay=5',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true},
            {label: '订单号', name: 'orderNo', index: 'orderNo', width: 110},
            {label: '用户编号', name: 'userId', index: 'userId', width: 50},
            {label: '手机号', name: 'userPhone', index: 'userPhone', width: 50},
            {label: '产品名称', name: 'productName', index: 'productName', width: 50},
            {label: '金额', name: 'amount', index: 'amount', width: 50},
            {label: '费率', name: 'rate', index: 'rate', width: 50},
            {label: '手续费', name: 'rateValue', rateValue: 'rate', width: 50},
            {label: '实付', name: 'realAmount', index: 'realAmount', width: 50},
            {label: '收货人', name: 'receiveUser', index: 'receiveUser', width: 50},
            {label: '收货人手机', name: 'receivePhone', index: 'receivePhone', width: 50},
            {label: '数量', name: 'number', index: 'number', width: 50},
            {
                label: '订单状态', name: 'status', index: 'status', width: 60,
                formatter: function (value) {
                    if (value == '1') {
                        return '未支付';
                    } else if (value == '2') {
                        return '支付完成';
                    } else if (value == '3') {
                        return '已取消';
                    } else if (value == '4') {
                        return '已完成';
                    } else if (value == '5') {
                        return '待发货';
                    } else if (value == '6') {
                        return '已发货';
                    } else if (value == '9') {
                        return '退款处理';
                    } else if (value == '10') {
                        return '退款中';
                    } else if (value == '11') {
                        return '退款完成';
                    }
                    return value;
                }
            },
            {label: '地址', name: 'addressStr', index: 'addressStr', width: 80},
            {label: '物流单号', name: 'logisticsno', index: 'logisticsno', width: 80},
            {label: '商家编号', name: 'merchantId', index: 'merchantId', width: 80},
            {label: '商家名称', name: 'merchantName', index: 'merchantName', width: 80},
            {label: '备注', name: 'remark', index: 'remark', width: 80},
            {
                label: '下单时间', name: 'createDate', index: 'createDate', width: 80,
                formatter: function (value) {
                    return transDate(value);
                }
            },
            {label: '支付时间', name: 'payDate', index: 'payDate', width: 60}

        ]
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        logistics: {},
        orderspc: {},
        detail: false,
        showadd: false,
        showconfirm: false,
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            orderNo: '',
            userPhone: '',
            payStatus: '',
            status: '',
            date:''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.showadd = true;
            vm.orderspc = {};
            vm.showconfirm = false;
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.showadd = true;
            vm.title = "修改";
            vm.showconfirm = false;
            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            let url = vm.orderspc.id == null ? "../orderamount/save" : "../orderamount/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.orderspc),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../orderamount/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../orderamount/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.orderspc = r.orderspc;
                }
            });
        },
        openDetail:function(){
            let ids = getSelectedRow("#jqGrid");
            if (ids == null) {
                return;
            }
            Ajax.request({
                url: "../orderspc/info/" + ids,
                async: false,
                successCallback: function (r) {
                    vm.orderspc = r.orderspc;
                }
            });
            vm.openSpe(vm.orderspc.orderNo)
        },
        openSpe: function (orderNo) {
            openWindow({
                type: 2,
                area: ['1000px', '600px'],
                title: '订单详情',
                content: 'orderdetail.html?orderNo='+orderNo,
                btn: ['确定', '取消'],
                btn1: function (index) {
                    layer.close(index);
                }
            })
        },
        reload: function (event) {
            vm.showList = true;
            vm.logistics = {};
            vm.showconfirm = false;
            vm.showadd = false;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'orderNo': vm.q.orderNo,
                    'userPhone': vm.q.userPhone,
                    'payStatus': vm.q.payStatus,
                    'status': vm.q.status,
                    'date':vm.q.date==''?'':new Date(vm.q.date).dateFormat("yyyy-MM-dd")
                },
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                orderNo: '',
                userPhone: '',
                payStatus: '',
                status: '',
                date:''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        lookConfirm: function (rowId) {
            console.log(rowId)
            confirm('确定要兑换么？', function () {
                Ajax.request({
                    url: "../orderamount/confirm",
                    params: JSON.stringify(rowId),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });

        },
        send: function (rowId) {
            Ajax.request({
                type: "POST",
                url: "../logistics/saveSpc",
                contentType: "application/json",
                params: JSON.stringify(vm.orderspc),
                successCallback: function (r) {
                    vm.reload();
                }
            });

        },
        sendGoods: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }

            // openWindow({
            //     type: 2,
            //     title: '发货管理',
            //     content: '..//ordershipping.html?orderid=' + id
            // })
            console.log(id);
            vm.showList = false;
            vm.title = "发货";
            Ajax.request({
                url: "../order/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.orderspc = r.order;
                }
            });
        },
        saveSend: function (event) {
            if (vm.orderspc.logisticsno == null) {
                alert("请填写物流单号");
                return;
            }
            if (vm.logistics.name == null) {
                alert("请填写物流名称");
                return;
            }
            if (vm.logistics.statu == null) {
                vm.logistics.statu = 1;
            }
            vm.logistics.orderno = vm.orderspc.orderNo;
            vm.logistics.logisticsNo = vm.orderspc.logisticsno;
            vm.logistics.pid = vm.orderspc.productId;
            vm.logistics.pname = vm.orderspc.productName;
            vm.logistics.num = vm.orderspc.number;
            vm.logistics.code = vm.orderspc.code;
            vm.logistics.userId = vm.orderspc.userId;
            vm.logistics.userName = vm.orderspc.userName;
            vm.logistics.phone = vm.orderspc.userPhone;
            vm.logistics.type = 3;
            vm.logistics.remark = vm.orderspc.remark;
            Ajax.request({
                type: "POST",
                url: "../logistics/saveSpc",
                contentType: "application/json",
                params: JSON.stringify(vm.logistics),
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        exportPro: function () {
            var strhref = "../orderamount/export?type=3" ;
            if (vm.q.userPhone != '')
                strhref += "&userPhone=" + vm.q.userPhone;
            if (vm.q.status != '')
                strhref += "&status=" + vm.q.status;
            if (vm.q.payStatus != '')
                strhref += "&payStatus=" + vm.q.payStatus;
            if (vm.q.orderNo != '')
                strhref += "&orderNo=" + vm.q.orderNo;
                strhref += "&payWay=" + 5;
            let index = $("#jqGrid").jqGrid('getGridParam', 'page');
            strhref += "&index=" + index;
            strhref += "&size=" + 50;
            location.href = strhref;
        },
    }
});